/**
 * http请求封装
 */
export default function ({ $cookies, app: { $axios, $msg } }) {
    // 配置
    $axios.defaults.withCredentials = true;
    $axios.defaults.timeout = 20000;

    // 请求拦截
    $axios.onRequest((config) => {

        const vuex = $cookies.get('vuex');

        if (vuex) {
            if (vuex.token) config.headers.Authorization = `Bearer ${vuex.token}`;
        }

        return config
    })

    // 错误回调
    $axios.onError((error) => {
        const statusCode = Number.parseInt(error.response && error.response.status)
        const statusCustomText = error.response && error.response.statusCustomText
        const message = statusCustomText || "服务器发送错误，请稍后再试";

        switch (statusCode) {
            case 401:
                $msg.error("没有登录");
                break;
            case 419:
                $msg.error("页面过期");
                break;
            case 422 || 423 || 429:
                $msg.error(statusCustomText);
                break;
            default:
                $msg.error(message);
                break;
        }
    })

    // 响应拦截
    $axios.onResponse((res) => {
    })
}

